$(function () {
    loading();
    $("#datepicker").val("");
    $("#datepicker2").val("");
    ajaxExtractRecords(1, null, null);
});

/**
 * 验证开始时间是否大于结束时间
 */
function validateTime() {
    var startTime = $("#datepicker").val();
    var endTime = $("#datepicker2").val();
    if (isNotEmpty(startTime) && isNotEmpty(endTime)) {
        if (startTime > endTime) {
            alertBox("开始时间不能大于结束时间", "error");
        }
    }
}

/**
 * 提现记录  按日期查询提现记录
 * @param page        当前页码
 * @param startTime    开始时间
 * @param endTime    结束时间
 */
function ajaxExtractRecords(page, startTime, endTime) {
    var url = "/Lvscore_User_M/adScore/ajaxExtractRecords";
    $.post(
        url,
        {
            'page': page,
            'startTime': startTime,
            'endTime': endTime
        },
        function (data) {
            /*map：
             sumpage：总页数
             nowpage：页码
             userExtractList：List<UserExtractDto>  提现记录列表
             */
            if (data.nowpage == 1) {
                $(".tcdPageCode").empty().unbind();
            }
            $("#info").empty();
            $("#info_head").empty();
            if (data.userExtractList.length == 0) {
                $(".table").css("border","none");
                $("#info").html('<p class="noResult" style="padding-top:20px;"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">未查询到任何数据</p>');
            }else{
                $("#info_head").append(
                '<th>分类</th>'+
                '<th>创建日期</th>'+
                '<th>名称</th>'+
                '<th>对方</th>'+
                '<th>金额明细</th>'+
                '<th>状态</th>'+
                '<th>操作</th>')
                $.each(data.userExtractList, function (i, e) {
                    $("#info").append(
                        "<tr>" +
                        "<td>" + e.targetName + "</td>" +
                        "<td>" + e.createTime + "</td>" +
                        "<td>提现-" + e.constumName + "</td>" +
                        "<td>" + e.targetName + "|..." + e.bankCardNum + "|" + e.accountUserName + "</td>" +
                        "<td>" + e.money + "</td>" +
                        "<td>" + getStatus(e.status) + "</td>" +
                        "<td><a href='javascript:;' onclick='getHref(" + e.id + "," + e.status + ")' class='inline-block border_radius marginR-10'>详情</a>" +
                        "<span class='inline-block border_radius handes'><a href='javaScript:void(0)' onclick='addNote(" + e.id + ")'>备注</a></span>" +
                        "<input id='noteId_" + e.id + "' type='hidden' value='" + e.constumName + "'/>" +
                        "<input id='note_" + e.id + "' type='hidden' value='" + e.note + "'/></td>" +
                        "</tr>"
                    );
                });
            }
            //分页
            if ($(".tcdPageCode").html() == "") {
                createPage(data.sumpage, data.nowpage, startTime, endTime);
            }
            $(".loadingBox").hide();
        },
        "json"
    );
}
function addNote(id) {
    var name = $("#noteId_" + id).val();
    var note = $("#note_" + id).val();
    if (note == null || note == 'null') {
        note = '';
    }
    $("#exId").val(id);
    $("#note").val(note);
    $("#noteName").text(name);
    $(".remarks").show();
}

function noteClose() {
    $(".remarks").hide();
}

function getStatus(status) {
    if (status == 1) {
        return '提现成功';
    } else if (status == 0) {
        return '银行处理中';
    } else if (status == -1) {
        return '提现失败';
    }
}

function getHref(id, status) {
    var cookie = new sweetCookie();
    cookie.setCookie("ext_id", encodeBase64("extract_" + id, 3), 3 * 10 * 60 * 1000);   //过期时间30min
    if (status == 1) {
        window.location.href = '/Lvscore_User_M/userWeb/views/ADadmin/accounterMoney/recordDetailSuccess.html';
    } else if (status == -1) {
        window.location.href = '/Lvscore_User_M/userWeb/views/ADadmin/accounterMoney/recordDetailFail.html';
    } else if (status == 0) {
        window.location.href = '/Lvscore_User_M/userWeb/views/ADadmin/accounterMoney/recordDetaildoing.html';
    }
}

function search() {
    ajaxExtractRecords(1, $("#datepicker").val(), $("#datepicker2").val());
}

function addNoteExecute() {
    var id = $("#exId").val();
    var note = $("#note").val();
    if (note.length <= 100) {
        noteClose();
        ajaxAddNote(id, note);
    }
}

var flag = false;
function getUserNum(text) {
    var length = text.length;
    if (length > 100) {
        $("#outOfLength").empty();
        $("#outOfLength").append("<span style='color:red; font-size:14px'>超出字数限制</span>");
        flag = true;
    } else {
        if (flag) {
            $("#outOfLength").html("（还可写<span id='useNum'></span>个字）");
        }
        $("#useNum").text(100 - length);
    }
}

/**
 * 给提现记录添加备注
 * @param id    提现记录id
 * @param note    提现记录备注
 */
function ajaxAddNote(id, note) {
    var url = "/Lvscore_User_M/adScore/ajaxAddNote";
    $.post(
        url,
        {
            'id': id,
            'note': note
        },
        function (data) {
            if (data == 1) {
                alertBox('添加成功');
                $("#note_" + id).val(note);
            } else if (data == -1) {
                alertBox('添加失败', '');
            } else {
                alertBox('服务器异常，添加失败', '');
            }
        },
        "json"
    );
}

function createPage(pages, pageNum, startTime, endTime) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxExtractRecords(p, startTime, endTime);
            }
        });
    }
}
